package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppUserMapper;
import com.ruoyi.system.domain.AppUser;
import com.ruoyi.system.service.IAppUserService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * app用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-13
 */
@Service
public class AppUserServiceImpl implements IAppUserService 
{
    @Resource
    private AppUserMapper appUserMapper;

    /**
     * 查询app用户
     * 
     * @param id app用户ID
     * @return app用户
     */
    @Override
    public AppUser selectAppUserById(Long id)
    {
        return appUserMapper.selectAppUserById(id);
    }

    /**
     * 查询app用户列表
     * 
     * @param appUser app用户
     * @return app用户
     */
    @Override
    public List<AppUser> selectAppUserList(AppUser appUser)
    {
        return appUserMapper.selectAppUserList(appUser);
    }

    /**
     * 新增app用户
     * 
     * @param appUser app用户
     * @return 结果
     */
    @Override
    public int insertAppUser(AppUser appUser)
    {
        appUser.setCreateTime(DateUtils.getNowDate());
        return appUserMapper.insertAppUser(appUser);
    }

    /**
     * 修改app用户
     * 
     * @param appUser app用户
     * @return 结果
     */
    @Override
    public int updateAppUser(AppUser appUser)
    {
        appUser.setUpdateTime(DateUtils.getNowDate());
        return appUserMapper.updateAppUser(appUser);
    }

    /**
     * 删除app用户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAppUserByIds(String ids)
    {
        return appUserMapper.deleteAppUserByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除app用户信息
     * 
     * @param id app用户ID
     * @return 结果
     */
    @Override
    public int deleteAppUserById(Long id)
    {
        return appUserMapper.deleteAppUserById(id);
    }

    /**
     * 通过账号查询用户
     * @param userName
     * @return
     */
    @Override
    public AppUser getAppUserByUserName(String userName) {
        AppUser appUser = new AppUser();
        appUser.setUserName(userName);
        List<AppUser> appUsers = appUserMapper.selectAppUserList(appUser);
        if (appUsers.size()==0) {
            return null;
        }else if (appUsers.size()==1){
            return appUsers.get(0);
        }else {
            return null;
        }
    }

    /**
     * 通过uuid查询用户
     * @param uuid
     * @return
     */
    @Override
    public AppUser getAppUserByUuid(String uuid) {
        AppUser appUser = new AppUser();
        appUser.setUuid(uuid);
        appUser.setType(1);
        List<AppUser> appUsers = appUserMapper.selectAppUserList(appUser);
        if (appUsers.size()==1) {
            return appUsers.get(0);
        }else {
            return null;
        }
    }

    /**
     * 通过账号密码查询用户
     * @param appUser
     * @return
     */
    @Override
    public AppUser getAppUserByUserNameAndPassword(AppUser appUser) {
        appUser.setType(0);
        List<AppUser> appUsers = appUserMapper.getAppUserByUserNameAndPassword(appUser);
        if (appUsers.size()==1) {
            return appUsers.get(0);
        }else if (appUsers.size()>0){
            return null;
        }
        return null;
    }

    /**
     * 统计数据
     * @return
     */
    @Override
    public JSONObject statistical() {
        AppUser userMonth = new AppUser();
        userMonth.setType(0);
        AppUser userWeek= new AppUser();
        userWeek.setType(0);
        AppUser userToday = new AppUser();
        userToday.setType(0);
        AppUser user = new AppUser();
        user.setType(0);
        List<AppUser> month = appUserMapper.getMonth(userMonth);
        List<AppUser> week = appUserMapper.getWeek(userWeek);
        List<AppUser> today = appUserMapper.getToday(userToday);
        List<AppUser> register = appUserMapper.selectAppUserList(user);
        AppUser unuserMonth = new AppUser();
        unuserMonth.setType(1);
        AppUser unuserWeek= new AppUser();
        unuserWeek.setType(1);
        AppUser unuserToday = new AppUser();
        unuserToday.setType(1);
        AppUser unuser = new AppUser();
        unuser.setType(1);
        List<AppUser> unmonth = appUserMapper.getMonth(unuserMonth);
        List<AppUser> unweek = appUserMapper.getWeek(unuserWeek);
        List<AppUser> untoday = appUserMapper.getToday(unuserToday);
        List<AppUser> unregister = appUserMapper.selectAppUserList(unuser);
        JSONObject object = new JSONObject();
        object.put("register",register.size());
        object.put("month",month.size());
        object.put("week",week.size());
        object.put("today",today.size());
        object.put("unregister",unregister.size());
        object.put("unmonth",unmonth.size());
        object.put("unweek",unweek.size());
        object.put("untoday",untoday.size());
        return object;
    }
}
